/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.action;

import com.cunyu.bean.Form;
import com.cunyu.bean.Muser;
import com.cunyu.bean.ReqData;
import com.cunyu.bean.RespData;
import com.cunyu.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 手机用户管理
 * @author win
 * 2019/04/26-17:02
 */
@Controller
public class SuserController extends BaseController{

    @Resource
    PushService pushService ;

    @Resource
    SuserService suserService ;

    @Resource
    AccountLogService accountLogService ;

    @Resource
    TagService tagService ;

    @Resource
    InvitePayService invitePayService ;

    @RequestMapping("/suser/page")
    public String page(Model model, ReqData reqData, Form form){
        RespData<List<Map<String, Object>>> page = suserService.getPage(reqData,form);
        model.addAttribute("respData",page) ;
        model.addAttribute("form",form) ;
        return "suser/list" ;
    }

    @RequestMapping("/suser/fund")
    public String fund(Model model, ReqData reqData, Form form){
        RespData<List<Map<String, Object>>> page = invitePayService.getPage(reqData,form);
        model.addAttribute("respData",page) ;
        model.addAttribute("form",form) ;
        return "suser/fund" ;
    }

    @RequestMapping("/suser/user")
    public String user(Model model,int id){
        Map<String, Object> user = suserService.getUser(id);
        Map<String, Map<String, Object>> tagInfo = tagService.getTagInfo();
        model.addAttribute("respData",user) ;
        model.addAttribute("tagInfo",tagInfo) ;
        return "suser/info" ;
    }

    @RequestMapping("/suser/account")
    public String account(Model model,Form form,ReqData reqData){
        Muser user = getOnlineUser();
        if(user.getRole() != 1){
            return "404" ;
        }
        RespData<List<Map<String, Object>>> page = accountLogService.logPage(form, reqData);
        model.addAttribute("respData",page) ;
        model.addAttribute("form",form) ;
        return "suser/account" ;
    }

    @RequestMapping("/suser/invite")
    public String invite(Model model ,ReqData reqData,Form form){
        RespData<List<Map<String, Object>>> view = invitePayService.view(reqData, form);
        Map<String, Object> count = invitePayService.count(form);
        model.addAttribute("respData",view) ;
        model.addAttribute("reqData",reqData) ;
        model.addAttribute("count",count) ;
        model.addAttribute("form",form) ;
        return "suser/invite" ;
    }

    @RequestMapping("/suser/disable")
    public void disable(HttpServletResponse response,int state,int id){
        suserService.disable(state,id);
        printOK(response,"success",1);
    }

    @RequestMapping("/suser/withdraw")
    public void withdraw(HttpServletResponse res , int uid,int money,String des) throws IOException {
        if(money <= 0){
            res.setStatus(500) ;
            return;
        }
        invitePayService.withdraw(uid,money,des);
        res.sendRedirect("/suser/invite?id="+uid);
    }

    @RequestMapping("/suser/check")
    public void check(HttpServletResponse response,int id,int state){
        int p = suserService.check(id,state);
        String info = p == 1 ?"您的认证信息已通过":"您的认证信息未通过，请重新提交完整信息" ;
        pushService.push("认证信息",info,id);
        printOK(response,"success",1);
    }
    // 账号 ， account ， accountlog 表信息

}
